﻿using Blog.Core.Common;
using Microsoft.AspNetCore.Builder;
using System.Linq;

namespace Blog.Core.Extensions
{
    /// <summary>
    /// Api接口版本 自定义
    /// </summary>
    public enum ApiVersions
    {
        /// <summary>
        /// v1 版本
        /// </summary>
        v1 = 1,

        /// <summary>
        /// v2 版本
        /// </summary>
        v2 = 2,
    }

    public static class MiddlewareExtensions
    {
        /// <summary>
        /// 配置Swagger中间件
        /// </summary>
        /// <param name="app"></param>
        public static IApplicationBuilder UseSwaggerSetup(this IApplicationBuilder app)
        {
            app.UseSwagger();
            return app.UseSwaggerUI(c =>
            {
                var ApiName = Appsettings.app(new string[] { "Startup", "ApiName" });

                // 遍历出全部的版本，做文档信息展示
                typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{ApiName} {version}");
                });

                // 路径配置，设置为空，表示直接在根域名（localhost:8001）访问该文件
                // 注意localhost:8001/swagger是访问不到的，去launchSettings.json把launchUrl去掉，如果你想换一个路径，直接写名字即可，比如直接写c.RoutePrefix = "doc";
                c.RoutePrefix = "";
            });
        }
    }
}
